home *** CD-ROM | disk | FTP | other *** search
/ Alde ADA 1: #1 / CCCC 8804 Volume 1 Number 1 - Alde.iso / C / MISC / FUNC / PROFF.ARC / PROFF.C < prev    next >
Encoding:
Text File  |  1988-02-21  |  9.1 KB  |  391 lines

  1. char *version = "v.1.1";
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include "debug.h"
  6. #include "defs.h"
  7. #include "lookup.h"
  8.  
  9. /*
  10.  * G L O B A L S
  11.  *
  12.  */
  13. #ifndef vms
  14. #define globaldef
  15. #endif
  16.  
  17.  /* next available char; init = 0 */
  18. globaldef int bp = -1;
  19.  
  20.  /* pushed-back characters */
  21. globaldef char buf[BUFSIZE];
  22.  
  23.  /* stack of file descriptors */
  24. globaldef FILE *infile[NFILES];
  25.  
  26.  /* current file is infile[level] */
  27. globaldef int level;
  28.  
  29.  /* stack of output file descriptors */
  30. globaldef FILE *outfile[NFILES];
  31.  
  32.  /* current output is outfile[olevel]; */
  33. globaldef int olevel;
  34.  
  35.  /* current output file pointer */
  36. globaldef FILE *poutput;
  37.  
  38.  /* number registers a..z */
  39. globaldef int nr[26] = {
  40.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  42. };
  43.  
  44.  /* system registers a..z */
  45. globaldef int sr[26] = {
  46.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  48. };
  49.  
  50.  /* last char position in outbuf; init = 0 */
  51. globaldef int outp = 0;
  52.  
  53.  /* width of text currently in outbuf; init = 0 */
  54. globaldef int outw = 0;
  55.  
  56.  /* number of words in outbuf; init = 0 */
  57. globaldef int outwds = 0;
  58.  
  59.  /* lines to be filled collect here */
  60. globaldef char outbuf[MAXOUT];
  61.  
  62.  /* word in outbuf; init=0 */
  63.  /* current output page number; init = 0 */
  64. globaldef int curpag = 0;
  65.  
  66.  /* next output page number; init = 1 */
  67. globaldef int newpag = 1;
  68.  
  69.  /* next line to be printed; init = 0 */
  70. globaldef int lineno = 0;
  71.  
  72.  /* page length in lines; init = PAGELEN = 66 */
  73. globaldef int plval = PAGELEN;
  74.  
  75.  /* page length save area */
  76. globaldef int savpl = PAGELEN;
  77.  
  78.  /* margin before and including header */
  79. globaldef int m1val = 3;
  80.  
  81.  /* margin after header */
  82. globaldef int m2val = 2;
  83.  
  84.  /* margin after last text line */
  85. globaldef int m3val = 2;
  86.  
  87.  /* bottom margin, including footer */
  88. globaldef int m4val = 3;
  89.  
  90.  /* last live line on page, = plval-m3val-m4val */
  91. globaldef int bottom = PAGELEN - 5;
  92.  
  93.  /* top of page title for even pages;init=NEWLINE */
  94. globaldef char ehead[MAXLINE];
  95.  
  96.  /* top of page title for odd  pages;init=NEWLINE */
  97. globaldef char ohead[MAXLINE];
  98.  
  99.  /* left,right margins for even header;init=inval,rmval */
  100. globaldef int ehlim[2] = {0, PAGEWIDTH};
  101.  
  102.  /* left,right margins for odd  header;init=inval,rmval */
  103. globaldef int ohlim[2] = {0, PAGEWIDTH};
  104.  
  105.  /* bot of page title for even pages;init=NEWLINE */
  106. globaldef char efoot[MAXLINE];
  107.  
  108.  /* bot of page title for odd  pages;init=NEWLINE */
  109. globaldef char ofoot[MAXLINE];
  110.  
  111.  /* left,right margins for even footer;init=inval,rmval */
  112. globaldef int eflim[2] = {0, PAGEWIDTH};
  113.  
  114.  /* left,right margins for odd  footer;init=inval,rmval */
  115. globaldef int oflim[2] = {0, PAGEWIDTH};
  116.  
  117.  /* flag for pausing between pages */
  118. globaldef int stopx = 0;
  119.  
  120.  /* first page to begin printing with */
  121. globaldef int frstpg = 0;
  122.  
  123.  /* last page to be printed */
  124. globaldef int lastpg = HUGE;
  125.  
  126.  /* flag to indicate whether page should be printed */
  127. globaldef int print = YES;
  128.  
  129.  /* number of blanks to offset page by; init = 0 */
  130. globaldef int offset = 0;
  131.  
  132.  /* verbose option; init = NO */
  133. globaldef int verbose = NO;
  134.  
  135.  /* bolding option; init = YES; */
  136. globaldef char bolding = YES;
  137.  
  138.  /* fill if YES; init = YES */
  139. globaldef int fill = YES;
  140.  
  141.  /* current line spacing; init = 1 */
  142. globaldef int lsval = 1;
  143.  
  144.  /* current indent; >= 0; init = 0 */
  145. globaldef int inval = 0;
  146.  
  147.  /* current right margin; init = PAGEWIDTH = 60 */
  148. globaldef int rmval = PAGEWIDTH;
  149.  
  150.  /* current temporary indent; init = 0 */
  151. globaldef int tival = 0;
  152.  
  153.  /* number of lines to center; init = 0 */
  154. globaldef int ceval = 0;
  155.  
  156.  /* flag for continuous center */
  157. globaldef char CEon = FALSE;
  158.  
  159.  /* number of lines to underline; init = 0 */
  160. globaldef int ulval = 0;
  161.  
  162.  /* flag for continuous underline */
  163. globaldef char ULon = FALSE;
  164.  
  165.  /* number of lines to boldface; init = 0 */
  166. globaldef int boval = 0;
  167.  
  168.  /* flag for continuous bolding */
  169. globaldef char BDon = FALSE;
  170.  
  171.  /* justification types for heads and foots; */
  172.  /* init = LEFT, CENTER, RIGHT */
  173. globaldef int tjust[3] = {LEFT, CENTER, RIGHT};
  174.  
  175.  /* number of lines to blank suppress; init=0 */
  176. globaldef int bsval = 0;
  177.  
  178.  /* right justify filled lines if YES; init=YES */
  179. globaldef int rjust = YES;
  180.  
  181.  /* tab stops; init every 8 spaces */
  182. globaldef int tabs[INSIZE];
  183.  
  184.  /* line control character; init = PERIOD */
  185. globaldef char cchar = '.';
  186.  
  187.  /* universal escape - init = UNDERBAR */
  188. globaldef char genesc = '_';
  189.  
  190.  /* character used to underline a BLANK; init = BLANK */
  191. globaldef char ulblnk = ' ';
  192.  
  193.  /* scratch arrays for use by various routines */
  194. globaldef char tbuf1[MAXLINE];
  195. globaldef char tbuf2[MAXLINE];
  196. globaldef char tbuf3[MAXLINE];
  197. globaldef char ttl[MAXLINE];
  198.  
  199.  /* flag to process runoff symbols only */
  200. globaldef char onlyrunoff = NO;
  201.  
  202.  /* Flag to turn paging off */
  203. globaldef char paging = YES;
  204.  
  205.  /* page number in roman numerals. Init = NO */
  206. globaldef char roman = NO;
  207.  
  208.  /* autopar flag. Init = NO */
  209. globaldef char autopar = NO;
  210.  
  211.  /* temporary indent value for autopar */
  212. globaldef int autoprv = 5;
  213.  
  214.  /* hash tables for macros and variables */
  215. globaldef struct hashlist *macrotab[HASHMAX];
  216. globaldef struct hashlist *gentab[HASHMAX];
  217.  
  218.  /* linked list entries for contents     */
  219. globaldef struct clist *chead = NULL;
  220. globaldef struct clist *clast = NULL;
  221.  
  222.  /* keep track of what is done - VERBOSE */
  223. globaldef int p_txtlines = 0;
  224. globaldef int p_outlines = 0;
  225. globaldef int p_outpages = 0;
  226. globaldef int p_memoryus = 0;
  227.  
  228. /*
  229.  * M A I N L I N E   OF   P R O F F
  230.  */
  231.  
  232. main(argc, argv)
  233. int argc;
  234. char *argv[];
  235. {
  236.    int i, j, val, type;
  237.    char *p, c;
  238.    FILE *fp;
  239.  
  240.    for (i = 1; i < argc; i++)
  241.    {
  242.       p = argv[i];
  243.       if (*p == '-')
  244.       {
  245.          ++p;
  246.          if (isalpha(*p))
  247.          {
  248.             c = *p++;
  249.             switch (c)
  250.             {
  251.              /* verbose stats */
  252.             case 'v':
  253.             case 'V':
  254.                verbose = TRUE;
  255.                break;
  256.              /* runoff only   */
  257.             case 'r':
  258.             case 'R':
  259.                onlyrunoff = TRUE;
  260.                break;
  261.              /* stop for page */
  262.             case 's':
  263.             case 'S':
  264.                stopx = 1;
  265.                break;
  266.              /* page offset   */
  267.             case 'p':
  268.             case 'P':
  269.                if (*p == 'o' || *p == 'O')
  270.                {
  271.                   p++;
  272.                   j = 0;
  273.                   val = getval(p, &j, &type);
  274.                   set(&offset, val, type, 0, 0, rmval - 1);
  275.                }
  276.                else
  277.                   usage();
  278.                break;
  279.              /* include file  */
  280.             case 'i':
  281.             case 'I':                 /* simulate .so <filename> */
  282.                pbstr("\n");
  283.                pbstr(p);
  284.                pbstr(".so ");
  285.                break;
  286.  
  287.              /* disable some  */
  288.             case 'd':
  289.             case 'D':
  290.                switch (*p)
  291.                {
  292.                case 'b':
  293.                case 'B':
  294.                   bolding = NO;
  295.                   break;
  296.                case 'p':
  297.                case 'P':
  298.                   paging = NO;
  299.                   break;
  300.                default:
  301.                   break;
  302.                }
  303.                break;
  304.  
  305.              /* garbage       */
  306.             default:
  307.                usage();
  308.             }
  309.          }
  310.          else
  311.             lastpg = atoi(p);
  312.       }
  313.       else if (*p == '+')
  314.       {
  315.          p++;
  316.          if ((frstpg = atoi(p)) == 0)
  317.             usage();
  318.       }
  319.       else
  320.          break;
  321.    }
  322.    if (i == argc)
  323.       usage();
  324.    if ((fp = fopen(argv[i], "r")) == NULL)
  325.    {
  326.       fprintf(stderr, "%s: cannot open.\n", argv[i]);
  327.       exit(1);
  328.    }
  329.    if (p = argv[++i])
  330.    {
  331.       if ((outfile[0] = fopen(p, "w")) == NULL)
  332.       {
  333.          fprintf(stderr, "%s: cannot create.\n\n", p);
  334.          exit(1);
  335.       }
  336.    }
  337.    else
  338.       outfile[0] = stdout;
  339.    /* set output file level */
  340.    olevel = 0;
  341.    poutput = outfile[0];
  342.    /*
  343.     * some minor initialisation 
  344.     */
  345.  
  346.    for (i = 0; i < INSIZE; i++)
  347.       if (i % 8 == 0)
  348.          tabs[i] = YES;
  349.       else
  350.          tabs[i] = NO;
  351.  
  352.    ehead[0] = '\n';
  353.    ehead[1] = EOS;
  354.    ohead[0] = '\n';
  355.    ohead[1] = EOS;
  356.    efoot[0] = '\n';
  357.    efoot[1] = EOS;
  358.    ofoot[0] = '\n';
  359.    ofoot[1] = EOS;
  360.  
  361.    /* initialise contents linked list */
  362.  
  363.    chead = (struct clist *) malloc(sizeof(struct clist));
  364.    clast = chead;
  365.    p_memoryus += sizeof(struct clist);
  366.  
  367.    doroff(fp);
  368.    brk();
  369.    if (plval <= 100 && (lineno > 0 | outp > 0))
  370.       space(HUGE);
  371.    putchar('\n');
  372.  
  373.    if (verbose)
  374.    {
  375.       fprintf(stderr, "proff read in %6d textlines to produce\n",
  376.               p_txtlines);
  377.       fprintf(stderr, "              %6d lines\n",
  378.               p_outlines);
  379.       fprintf(stderr, "              %6d pages of formatted text.\n",
  380.               p_outpages);
  381.       fprintf(stderr, "\n%d bytes of memory was required\n",
  382.               p_memoryus);
  383.       fprintf(stderr, "for internal tables and lists.\n");
  384.    }
  385. #ifdef vms
  386.    exit(1);
  387. #else
  388.    exit(0);
  389. #endif
  390. }
  391.